package com.geniusk.system.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import com.geniusk.common.core.JSONResult;
import com.geniusk.security.annotation.PreFuncAuth;
import com.geniusk.security.utils.SecurityUtil;
import com.geniusk.system.model.bo.model.SelectModelByIdBO;
import com.geniusk.system.model.bo.modelProperty.*;
import com.geniusk.system.model.pojo.ModelProperty;
import com.geniusk.system.service.ModelPropertyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 模型属性管理接口
 */
@RestController
@RequestMapping("/system/modelProperty")
public class ModelPropertyController {

    @Autowired
    private ModelPropertyService modelPropertyService;

    /**
     * ID查询模型属性列表
     */
    @PostMapping("/selectModelPropertyByModelId")
    @PreFuncAuth("SYS:MODEL:SELECT")
    public JSONResult selectModelPropertyByModelId(@Valid @RequestBody SelectModelByIdBO selectModelByIdBO) {
        List<ModelProperty> dictDataList = modelPropertyService.selectModelPropertyByModelId(selectModelByIdBO.getModelId());
        Map<String, Object> result = new HashMap<>();
        result.put("list",dictDataList);
        return JSONResult.ok(result);
    }

    /**
     * 设备ID查询模型属性
     */
    @PostMapping("/selectModelPropertyByDeviceId")
    @PreFuncAuth("SYS:DEVICE:SELECT")
    public JSONResult selectModelPropertyByDeviceId(@Valid @RequestBody SelectModelPropertyByDeviceIdBO selectModelPropertyByDeviceIdBO) {
        List<ModelProperty> modelPropertyList = modelPropertyService.selectModelPropertyByDeviceId(selectModelPropertyByDeviceIdBO.getDeviceId());
        Map<String, Object> result = new HashMap<>();
        result.put("list",modelPropertyList);
        return JSONResult.ok(result);
    }


    /**
     * ID查询模型属性
     */
    @PostMapping("/selectModelPropertyById")
    @PreFuncAuth("SYS:MODEL:SELECT")
    public JSONResult selectModelPropertyById(@Valid @RequestBody SelectModelPropertyByIdBO selectModelPropertyByIdBO) {
        ModelProperty modelProperty = modelPropertyService.selectModelPropertyById(selectModelPropertyByIdBO.getModelPropertyId());
        Map<String, Object> result = new HashMap<>();
        result.put("modelProperty",modelProperty);
        return JSONResult.ok(result);
    }

    /**
     * 添加模型属性
     */
    @PostMapping("/insertModelProperty")
    @PreFuncAuth("SYS:MODEL:INSERT")
    public JSONResult insertModelProperty(@Valid @RequestBody InsertModelPropertyBO insertModelPropertyBO) {
        ModelProperty modelProperty = JSONUtil.parse(insertModelPropertyBO).toBean(ModelProperty.class);
        Long dictDataId = IdUtil.getSnowflake().nextId();
        modelProperty.setModelPropertyId(dictDataId);
        modelProperty.setCreateBy(SecurityUtil.getUsername());
        int rows = modelPropertyService.insertModelProperty(modelProperty);
        Map<String,Object> result = new HashMap<>();
        result.put("rows",rows);
        return JSONResult.ok(result);
    }

    /**
     * 修改模型属性
     */
    @PostMapping("/updateModelProperty")
    @PreFuncAuth("SYS:MODEL:UPDATE")
    public JSONResult updateModelProperty(@Valid @RequestBody UpdateModelPropertyBO updateModelPropertyBO) {
        ModelProperty modelProperty = JSONUtil.parse(updateModelPropertyBO).toBean(ModelProperty.class);
        modelProperty.setUpdateBy(SecurityUtil.getUsername());
        int rows = modelPropertyService.updateModelProperty(modelProperty);
        Map<String,Object> result = new HashMap<>();
        result.put("rows",rows);
        return JSONResult.ok(result);
    }

    /**
     * 删除模型属性
     */
    @PostMapping("/deleteModelPropertyByIds")
    @PreFuncAuth("SYS:MODEL:DELETE")
    public JSONResult deleteModelPropertyByIds(@Valid @RequestBody DeleteModelPropertyByIdsBO deleteModelPropertyByIdsBO) {
        int rows = modelPropertyService.deleteModelPropertyByIds(deleteModelPropertyByIdsBO.getModelPropertyIds());
        Map<String,Object> result = new HashMap<>();
        result.put("rows",rows);
        return JSONResult.ok(result);
    }
}
